package yingyong;

import gongju.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@WebServlet("/insert")
public class insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf8");
        int brandid = Integer.parseInt(req.getParameter("brandid"));
        String time = req.getParameter("time");
        String type = req.getParameter("type");
        int i = 0;
        if ("yi".equals(type)){
            i=1;
        }else if ("er".equals(type)){
            i=2;
        }else if ("san".equals(type)){
            i=3;
        }
        int a = 0;
        String sql = "insert into attence values(?,?,?,?)";
        try {
            Connection conn = null;
            PreparedStatement pre = null;
            conn = DBUtil.getcoon();
            pre = conn.prepareStatement(sql);
            pre.setInt(1,0);
            pre.setString(2,time);
            pre.setInt(3,i);
            pre.setInt(4,brandid);
            a = pre.executeUpdate();
            if (a > 0) {
                resp.getWriter().write("<script>alert('添加成功');location.href='/select';</script>");
            }else {
                resp.getWriter().write("<script>alert('添加失败');location.href='/select';</script>");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        }
    }
